package com.yuanli.cosmeticmirror.model.impl;

import android.text.TextUtils;

import androidx.lifecycle.LifecycleOwner;

import com.alibaba.fastjson.JSON;
import com.comenjoysoft.baselibrary.listener.CallbackListener;
import com.comenjoysoft.baselibrary.util.SPUtils;
import com.orhanobut.logger.Logger;
import com.rxjava.rxlife.BaseScope;
import com.rxjava.rxlife.RxLife;
import com.yuanli.cosmeticmirror.base.Constant;
import com.yuanli.cosmeticmirror.base.HttpApi;
import com.yuanli.cosmeticmirror.bean.videoBean;
import com.yuanli.cosmeticmirror.bean.videoInfoBean;
import com.yuanli.cosmeticmirror.model.biz.IVideoBiz;


import rxhttp.wrapper.param.RxHttp;

public class VideoImpl extends BaseScope implements IVideoBiz {
    public VideoImpl(LifecycleOwner owner) {
        super(owner);
    }

    @Override
    public void like(String tutorialId, CallbackListener<videoBean> listener) {
        String userId = SPUtils.getInstance().getString(Constant.SP_USER_ACCOUNT);
        RxHttp.postJson(HttpApi.POST_LIKE)
                .add("userid", userId)
                .add("id", tutorialId)
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Logger.d(" like onSuccess: " + s);
                    videoBean response = JSON.parseObject(s, videoBean.class);
                    if (response.getCode() == 1) {
                        listener.success(response);
                    } else {
                        listener.failed(null);
                    }
                }, throwable -> {
                    Logger.d(" like onFailed: " + throwable.getMessage());
                    //请求失败
                    listener.failed(throwable.getMessage());
                });

    }

    @Override
    public void cancelLike(String tutorialId, CallbackListener<videoBean> listener) {
        RxHttp.postJson(HttpApi.POST_CANCEL_LIKE)
                .add("userid", SPUtils.getInstance().getString(Constant.SP_USER_ACCOUNT))
                .add("id", tutorialId)
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Logger.d(" cancelLike onSuccess: " + s);
                    videoBean response = JSON.parseObject(s, videoBean.class);
                    if (response.getCode() == 1) {
                        listener.success(response);
                    } else {
                        listener.failed(null);
                    }
                }, throwable -> {
                    Logger.d(" cancelLike onFailed: " + throwable.getMessage());
                    //请求失败
                    listener.failed(throwable.getMessage());
                });
    }

    @Override
    public void collect(String tutorialId, CallbackListener<videoBean> listener) {
        RxHttp.postJson(HttpApi.POST_COLLECT)
                .add("userid", SPUtils.getInstance().getString(Constant.SP_USER_ACCOUNT))
                .add("id", tutorialId)
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Logger.d(" collect onSuccess: " + s);
                    videoBean response = JSON.parseObject(s, videoBean.class);
                    if (response.getCode() ==1) {
                        listener.success(response);
                    } else {
                        listener.failed(null);
                    }
                }, throwable -> {
                    Logger.d(" collect onFailed: " + throwable.getMessage());
                    //请求失败
                    listener.failed(throwable.getMessage());
                });
    }

    @Override
    public void cancelCollect(String tutorialId, CallbackListener<videoBean> listener) {
        RxHttp.postJson(HttpApi.POST_DELETE_COLLECT)
                .add("userid", SPUtils.getInstance().getString(Constant.SP_USER_ACCOUNT))
                .add("id", tutorialId)
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Logger.d(" cancelCollect onSuccess: " + s);
                    videoBean response = JSON.parseObject(s, videoBean.class);
                    if (response.getCode() == 1) {
                        listener.success(response);
                    } else {
                        listener.failed(null);
                    }
                }, throwable -> {
                    Logger.d(" cancelCollect onFailed: " + throwable.getMessage());
                    //请求失败
                    listener.failed(throwable.getMessage());
                });
    }

    @Override
    public void getTutorialDetails(String tutorialId, CallbackListener<videoInfoBean> listener) {
        RxHttp.get(HttpApi.GET_TUTORIAL_DETAIL)
                .add("userid", SPUtils.getInstance().getString(Constant.SP_USER_ACCOUNT))
                .add("id", tutorialId)
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Logger.d(" getTutorialDetails onSuccess: " + s);
                    videoInfoBean response = JSON.parseObject(s, videoInfoBean.class);
                    if (response.getData() != null) {
                        listener.success(response);
                    } else {
                        listener.failed(null);
                    }
                }, throwable -> {
                    Logger.d(" getTutorialDetails onFailed: " + throwable.getMessage());
                    //请求失败
                    listener.failed(throwable.getMessage());
                });
    }

    @Override
    public void addRecord(String tutorialId) {
        RxHttp.postJson(HttpApi.POST_TUTORIAL_RECORD)
                .add("id", tutorialId)
                .asString()
                .as(RxLife.asOnMain(this)) //返回String类型
                .subscribe(s -> {          //订阅观察者，
                    Logger.d(" addRecord onSuccess: " + s);
                    //                    videoInfoBean response = JSON.parseObject(s, videoInfoBean.class);
                    //                    if (response.getData() != null) {
                    //                        listener.success(response);
                    //                    } else {
                    //                        listener.failed(null);
                    //                    }
                }, throwable -> {
                    Logger.d(" addRecord onFailed: " + throwable.getMessage());
                    //请求失败
                    //                    listener.failed(throwable.getMessage());
                });
    }
}
